// automatch
//value:nickname
//data:userId
var onAutoCompleteSelect = function(value, data) {
	add(data,value);
};

var options = {
	serviceUrl : '../user/nickname/getSuggestion.html',
	minChars : 1,
	maxHeight : 400,
	width : 154,
	zIndex : 9999,
	onSelect : onAutoCompleteSelect,
	noCache : true
};
// jquery.autocomplete与jquery-ui-1.8.16.custom.min.js中的autocomplete有冲突
// 所以将jquery.autocomplete中重新命名为autocomplete2
$("#nicknamequery").autocomplete2(options);


//点击"清空"按钮
$("#write_clean").click(function(){
	//清空联系人
	$("#receiveNicknames").empty();
	receiverIds.removeAll();
	receiveNames.removeAll();
	
	//清空标题
	$("#write_title").val("");
	
	//清空正文
	$("#write_content").val("");
	
	$("#write_send").attr("disabled",false);
	$("#write_backup").attr("disabled",false);
	
});

//点击发送
$("#write_send").click(function(){
	//联系人
	if(receiverIds.length==0){
		alert("请添加联系人");
		return;
	}
	//标题
	var title = $("#write_title").val();
	if(title==undefined || title=="" || title==null || title.trim()==""){
		alert("请填写标题");
		return;
	}
	
	var content = $("#write_content").val();
	if(content==undefined || content=="" || content==null || content.trim()==""){
		alert("请写点内容吧");
		return;
	}
	var urlstr = "../message/";
	var datastr = "title="+title+"&content="+content;
	if(receiverIds.length==1){
		urlstr +="sendByOne.html";
		datastr +="&receiveId="+receiverIds[0];
	}
	else{
		urlstr +="sendByGroup.html";
		for(var i=0;i<receiverIds.length;i++){
			datastr +="&receiveIds="+receiverIds[i];
		}
	}
	
	$("#write_send").attr("disabled",true);
	//发送到后台
	$.ajax({
		url : urlstr,
		data : datastr,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if(result==true){
				alert("发送成功");
				$("#write_clean").trigger("click");
			}
			else{
				alert("发送失败");
			}
		},

		error : function() {
			alert("连接服务器出错");
		}
	});
	
});

/**
 * 写信--> 保存为草稿
 */
$("#write_backup").click(function(){
	//联系人
	if(receiverIds.length==0){
		alert("请添加联系人");
		return;
	}
	//标题
	var title = $("#write_title").val();
	if(title==undefined || title=="" || title==null || title.trim()==""){
		alert("请填写标题");
		return;
	}
	
	var content = $("#write_content").val();
	if(content==undefined || content=="" || content==null || content.trim()==""){
		alert("请写点内容吧");
		return;
	}
	
	var datastr = "title="+title+"&content="+content;

	for(var i=0;i<receiverIds.length;i++){
		datastr +="&receiveIds="+receiverIds[i]+"&draftReceiveNames="+receiveNames[i];
	}
	
	//发送到后台保存
	$("#write_backup").attr("disabled",true);
	$.ajax({
		url : '../message/saveAsDraft.html',
		data : datastr,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if(result==true){
				alert("保存成功");
				$("#write_clean").trigger("click");
			}
			else{
				alert("保存失败");
			}
		},

		error : function() {
			alert("连接服务器出错");
		}
	});
	
});


/**
 * 在别的用户上直接点发送私信 
 */
$(document).ready(function(){
	var url = window.location.href;
	var id=-1,nickname="",scope="";
	
	if(url.indexOf("?")!=-1){
		var parastr = url.split("?")[1];
		var paras=parastr.split("&");
		//id
		if(parastr.indexOf("id")!=-1){
			id=parseInt(paras[0].split("=")[1]);
		}
		//nickname
		if(parastr.indexOf("nickname")!=-1){
			nickname=paras[1].split("=")[1];
		}
		//scope
		if(parastr.indexOf("scope")!=-1){
			scope=paras[2].split("=")[1];
		}
		//符合要求
		if(id!=-1 && nickname!="" && scope!=""){
			nickname=decodeURI(nickname);//不增加此项 会出现乱码
    		add(id,nickname);
		}
	}
});